﻿var pics = "";
$(function () {
    uploadPic();
    var caseXzId = getParam("CaseId");    
    $("#CaseId").val(caseXzId);
    $("#Id").val(0);    //线索只能新添，不能修改
    $("#btnSave").click(function () {        
        if(validate())
            save();
    });
    
});

//验证表单
function validate() {
    //验证表单
    var summary = $("#Summary").val();
    if ($.trim(summary).length == 0) {
        str = '内容不能为空';
        layer.tips(str, "#btnSave");
        $("#Summary").focus();
        return false;
    }
    return true;
}

function uploadPic() {    
    //参考https://github.com/think2011/localResizeIMG/wiki/2.-%E5%8F%82%E6%95%B0%E6%96%87%E6%A1%A3
    $("#uploaderInput").on("change", function () {        
        lrz(this.files[0], { width: 1000 })
            .then(function (rst) {
                rst.formData.append('fileLen', rst.fileLen);
                $.ajax({
                    type: "POST",
                    url: "/Picture/UploadFilesAjax",
                    contentType: false,
                    processData: false,
                    data: rst.formData,
                    success: function (item) {
                        //alert("上传成功！");
                        pics = pics + "|" + item;
                        $("#Pic").val(item);
                        
                        var tmpl = '<li class="weui-uploader__file" style="background-image:url(#url#)"></li>';                    
                        $("#uploaderFiles").append($(tmpl.replace('#url#', '/UploadFiles/' + item)));
                       

                    },
                    error: function () {
                        alert("上传失败!");
                    }
                });
            })
            .catch(function (err) {
                // 处理失败会执行
            })
            .always(function () {
                // 不管是成功失败，都会执行
            });
    });
}


//保存
function save() {
    var postData = {
        "Id": $("#Id").val(),
        "XSXXInfoId": $("#CaseId").val(),       
        "Summary": $("#Summary").val(),
        "ReporterName": $("#ReporterName").val() == null ? "" : $("#ReporterName").val(),
        "ReporterTel": $("#ReporterTel").val()== null ? "" : $("#ReporterTel").val(),
        "Voice": $("#Voice").val()== null ? "" : $("#Voice").val(),
        "Pic": pics,
        "Localization": $("#Localization").val() == null ? "" : $("#Localization").val()
         };    
    $.ajax({
        type: "Post",
        url: "/XSXXInfo/AddClue",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {                
                var $toast = $('#toast');
                if ($toast.css('display') != 'none') return;
                $toast.fadeIn(50);
                setTimeout(function () {
                    $toast.fadeOut(100);
                }, 2000);

                clearTable();//不清空，直到跳转后，后退还可见数据
                window.location.href = "/XsxxInfo/Index" //跳转页面
            } else {                
                layer.tips(data.message, "#btnSave");
                
            };
        }
    });
};

//清空表单
function clearTable() {   
    $("#Summary").val("");
    $("#ReporterName").val("");
    $("#ReporterTel").val("");
    $("#Pic").val("");
}

//获取URL上的参数
function getParam(paramName) {
    paramValue = "";
    isFound = false;
    if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
        arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&");
        i = 0;
        while (i < arrSource.length && !isFound) {
            if (arrSource[i].indexOf("=") > 0) {
                if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) {
                    paramValue = arrSource[i].split("=")[1];
                    isFound = true;
                }
            }
            i++;
        }
    }
    return paramValue;
}

function GetTime(date) {
    if (date == null) {
        date = new Date();
    }
    var y = date.getFullYear();
    var M = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    //var S = date.getTime() % 1000;
    var html = y + "-";
    if (M < 10) {
        html += "0";
    }
    html += M + "-";

    if (d < 10) {
        html += "0";
    }
    html += d + " ";
    if (h < 10) {
        html += "0";
    }
    html += h + ":";
    if (m < 10) {
        html += "0";
    }
    html += m + ":";
    if (s < 10) {
        html += "0";
    }
    html += s;
    //html += " ";
    //if (S < 100) {
    //    html += "0"
    //}
    //if (S < 10) {
    //    html += "0";
    //}
    //html += S;
    return html;
}